package com.chenfan.magic.entity;

import com.google.common.collect.Lists;
import lombok.Data;

import java.io.Serializable;
import java.util.List;
/**
 * @author lm
 */
@Data
public class TTLDetailModel implements Serializable {

    private static final long serialVersionUID = 1L;

    private Long startTime;

    private Long endTime;

    private Long ttl;

    private String explain;

    private List<TTLDetailModel> childTasks = Lists.newArrayList();

    public TTLDetailModel start() {
        this.startTime = System.currentTimeMillis();
        return this;
    }

    public TTLDetailModel start(String explain) {
        this.startTime = System.currentTimeMillis();
        this.explain = explain;
        return this;
    }

    public TTLDetailModel end(String explain) {
        this.endTime = System.currentTimeMillis();
        this.ttl = this.endTime - this.startTime;
        this.explain = explain;
        return this;
    }

    public TTLDetailModel end() {
        this.endTime = System.currentTimeMillis();
        this.ttl = this.endTime - this.startTime;
        return this;
    }

    public TTLDetailModel put(TTLDetailModel ttlDetailModel) {
        this.childTasks.add(ttlDetailModel);
        return this;
    }

    public TTLDetailModel putAll(List<TTLDetailModel> ttlDetailModels) {
        this.childTasks.addAll(ttlDetailModels);
        return this;
    }

    /**
     * 重新计算时间.
     * @return
     */
    public TTLDetailModel caculate() {
        Long tmpTtl = 0L;
        for (TTLDetailModel ttlDetailModel : this.childTasks) {
            tmpTtl += ttlDetailModel.getTtl();
        }
        this.ttl = tmpTtl;
        return this;
    }
}
